home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 13453 < prev    next >
Encoding:
Text File  |  1996-08-05  |  5.9 KB  |  154 lines

  1. Newsgroups: comp.object,comp.lang.eiffel,comp.lang.c++,comp.lang.beta,comp.lang.java,comp.lang.sather
  2. Path: lego.wes.mot.com!mothost!schbbs!news
  3. From: shang@corp.mot.com (David L. Shang)
  4. Subject: Re: What Should An Exception Handling Do? -- Clarification of rules
  5. Reply-To: shang@corp.mot.com
  6. Organization: MOTOROLA 
  7. Date: Mon, 25 Mar 1996 16:00:11 GMT
  8. Message-ID: <1996Mar25.160011.13921@schbbs.mot.com>
  9. References: <Doq3sv.MzA@research.att.com>
  10. Sender: news@schbbs.mot.com (SCHBBS News Account)
  11. Nntp-Posting-Host: 129.188.128.126
  12.  
  13. In article <Doq3sv.MzA@research.att.com> bs@research.att.com (Bjarne Stroustrup  
  14. <9758-26353> 0112760) writes:
  15. > billf@jovial.com (Bill Foote) writes
  16. >  > > quote explaining why C++ (and Java) uses the termination model
  17. >  > > of exception handling
  18. >  > 
  19. >  > A cynic might conclude something more along these lines:  "It would be 
  20. >  > hard to implement resumable exceptions in C++, so they decided to punt."
  21. > and that "cynic" would be wrong, guilty of not having done his homework,
  22. > and guilty of making unkind conjectures without basis in facts.
  23. >
  24.  
  25. But I don't see what's wrong with the cynic. It is true that to
  26. implement resumable exceptions in C++ is very hard. It is also
  27. true that resumable exception is useful, see below.
  28.   
  29. > Here, I'd like to quote a key section:
  30. >     Then, at the Palo Alto meeting in November 1991, we heard a
  31. >     brilliant summary of the arguments for termination semantics
  32. >     backed with both personal experience and data from Jim Mitchell
  33. >     (from Sun, formerly from Xerox PARC). Jim had used exception
  34. >     handling in half a dozen languages over a period of 20 years
  35. >     and was an early proponent of resumption semantics as one of
  36. >     the main designers and implementers of Xerox's Cedar/Mesa system.
  37. >     His message was
  38. >         ``termination is preferred over resumption; this is
  39. >         not a matter of opinion but a matter of years of
  40. >         experience. Resumption is seductive, but not valid.''
  41. >     He backed this statement with experience from several operating
  42. >     systems. The key example was Cedar/Mesa: It was written by people
  43. >     who liked and used resumption, but after ten years of use, there
  44. >     was only one use of resumption left in the half million line
  45. >     system -- and that was a context inquiry. Because resumption
  46. >     wasn't actually necessary for such a context inquiry, they removed
  47. >     it and found a significant speed increase in that part of the
  48. >     system. In each and every case where resumption had been used
  49. >     it had -- over the ten years -- become a problem and a more
  50. >     appropriate design had replaced it. Basically, every use of
  51. >     resumption had represented a failure to keep separate levels
  52. >     of abstraction disjoint
  53. >     Mary Fontana presented similar data from the TI Explorer system
  54. >     where resumption was found to be used for debugging only, Aron
  55. >      Insinga presented evidence of the very limited and nonessential
  56. >     use of resumption in DEC's VMS, and Kim Knuttilla related exactly
  57. >     the same story as Jim Mitchell for two large and long-lived
  58. >     projects inside IBM. To this we added a strong opinion in favor
  59. >      of termination based on experience at L.M.Ericsson relayed to
  60. >     us by Dag Bruck.
  61. >     Thus, the C++ committee endorsed termination semantics.
  62.  
  63. The key point is: WHAT IS AN EXCEPTION?
  64.  
  65. The definition:
  66.  
  67. >    "Exception handling is intended to allow code that has encountered 
  68. >a condition it cannot cope with to return to some other code that 
  69. >directly or indirectly invoked it. There is no way for an exception 
  70. >handler to request the thread of control to resume from the throw point. 
  71. >In other words, "throw" implements the termination model of exception 
  72. >handling."     -ARM, Ellis & Stroustrup, page 354
  73. >
  74.  
  75. narrows exceptions to error conditions only. If it is this case
  76. termination would be sufficient, and resumptions would be useless.
  77.  
  78. But an exception is not necessarily an error. Sometimes it is an
  79. condition that requires some extraordinary computation, a condition
  80. that is not supposed for a regular case, for example, to open a
  81. configuration file in an application directory and the file is not
  82. found. This condition requires a further processing, e.g. to look up
  83. the file in system directory. This is a quite common case in every
  84. system design. More examples:
  85.  
  86.       * the font does not exist and a query to the user is required to
  87.     get the substituting font;
  88.       * the input is in the wrong type and an input retry is required;
  89.       * the file is not associated with a default handler, would you
  90.     like to associate one and let me try to re-open the file?
  91.       * the format is not understood, would you suggest me a
  92.     converter?
  93.       * the embedded object is re-located, would give me the directory
  94.     to the new place?
  95.  
  96. These conditions are not errors but only require some extraordinary work.
  97. Similar examples were also given by Bill Foote in his previous post.
  98. It is not hard for people to figure out more examples that requires the
  99. following struture:
  100.  
  101.  result    = try do_something()
  102.       {
  103.         when condition1: some_extraordinary_work1; retry;
  104.         when condition2: some_extraordinary_work2; retry;
  105.         when condition3: some_extraordinary_work3; retry;
  106.         when condition4: return null;
  107.       }
  108.  
  109. is certainly better than:
  110.  
  111.     result = do_something();
  112.     exceptional_condition = check_error_message();
  113.     while (exceptional_condition)
  114.     {
  115.         if (exceptional_condition==condition4)
  116.         {
  117.         result = null;
  118.         break;
  119.         }
  120.         switch (exceptional_condition)
  121.         {
  122.         case condition1:
  123.             some_extraordinary_work1;
  124.             do_something();
  125.             break;
  126.         case condition2:
  127.             some_extraordinary_work2;
  128.             do_something();
  129.             break;
  130.         case condition3:
  131.             some_extraordinary_work3;
  132.             do_something();
  133.             break;
  134.         }
  135.     }
  136.  
  137. Oh, well! It takes me ten time longer to figure out the second piece of
  138. code and I am still not sure whether this code is correct or not. After
  139. a second look, yes, there are errors! "check_error_message()" should also
  140. be called in the loop to get the new exceptional condition after a retry.
  141.  
  142. David Shang
  143.  
  144.  
  145.